<?php

namespace App\Http\Controllers\Admin;

use App\Services\WxAutoreplyService;
use Illuminate\Http\Request;

class WxAutoreplyController extends BaseController
{
    private $wxAutoreplySer;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->wxAutoreplySer = new WxAutoreplyService();
    }

    /**
     * 自动回复列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxAutoreply::select('*');
            $keyword = $request->get('keyword', '');
            if ($keyword) {
                $query->where('name', 'like', '%' . $keyword . '%');
            }
            $data = $query->paginate($pageSize)->toArray();
            if ($data['data']){
                $replyList = $data['data'];
                foreach ($replyList as $k => $v){
                    $v['msg_type_str'] = $this->wxAutoreplySer->getMsgTypeStr($v['msg_type']);
                    $replyList[$k] = $v;
                }
                $data['data'] = $replyList;
            }

            return $data;
        }
        return view('admin.wxautoreply.index', $this->viewData);
    }

    /**
     * 添加或编辑页
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function form(Request $request)
    {
        $id = $request->get('id', 0);
        $autoreplyData = [
            'name' => '', 'keywords' => [], 'is_open' => 1,
            'start_time' => '', 'end_time' => '',
            'msg_type' => 'text', 'order_num' => 0, 'content' => '',
        ];
        if ($id) {
            $autoreply = \App\Models\WxAutoreply::with('material')->findOrFail($id);
            $autoreplyData = $autoreply->toArray();
        }
        //空素材数据
        if (empty($autoreplyData['material'])){
            $autoreplyData['material'] = [
                'title' => '',
                'description' => '',
                'music_url' => '',
                'hq_music_url' => '',
                'thumb_view_url' => '',
            ];
        }
        $this->viewData['autoreplyData'] = $autoreplyData;
        //回复类型列表
        $msgTypeList = $this->wxAutoreplySer->getMsgTypeList();
        $this->viewData['msgTypeList'] = $msgTypeList;
        return view('admin.wxautoreply.form', $this->viewData);
    }

    /**
     * 保存自动回复数据
     * @param \App\Http\Requests\AdminWxShorturlPost $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function save(\App\Http\Requests\AdminWxAutoreplyPost $request)
    {
        try {
            $rs = $this->wxAutoreplySer->save($request);
            if ($rs) {
                return $this->showMsg('success', ($request->input('id') ? '编辑' : '添加') . '关键字成功', [], 'admin.wxautoreply.index');
            }
            throw new \Exception(($request->input('id') ? '编辑' : '添加') . '关键字失败');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 删除自动回复
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        try {
            $id = $request->input('id', 0);
            if (!$id) {
                throw new \Exception('请指定要删除的自动回复');
            }
            $rs = $this->wxAutoreplySer->del($id);
            if (!$rs) {
                throw new \Exception('删除失败');
            }
            return $this->showMsg('success', '删除成功');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }
}
